package com.mx.tool.form.window.other;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.net.NetUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.server.SimpleServer;
import com.mx.tool.util.NotifyUtil;
import com.mx.tool.util.WebSourceDownloadUtil;
import com.sun.net.httpserver.HttpServer;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;

/**
 * @Author: 徐建鹏.
 * @create: 2021-11-19 15:29
 * @Description:
 */
public class WebSourceWindow {

    private JPanel jPanel;
    private JTextField dirText;
    private JButton chooseButton;
    private JTextField urlText;
    private JButton downloadButton;
    private JButton openButton;
    private JTextField textField1;
    private JButton stopButton;
    private JTextArea textArea1;
    private JButton startButton;
    private JTextField textField2;
    private HttpServer httpServer;
    private int port;

    public WebSourceWindow() {

        chooseButton.addActionListener(e -> {
            JFileChooser chooser = new JFileChooser();
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int returnVal = chooser.showOpenDialog(jPanel);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                //得到选择的文件
                File file = chooser.getSelectedFile();
                if (file == null || !file.exists()) {
                    NotifyUtil.error("文件夹选择错误");
                    return;
                }
                dirText.setText(file.getAbsolutePath());
            }
        });

        openButton.addActionListener(e -> {
            String dir = dirText.getText();
            if (StringUtils.isNotBlank(dir)) {
                File file = new File(dir);
                if (file.exists()) {
                    try {
                        Desktop.getDesktop().open(file);
                    } catch (IOException ex) {
                        NotifyUtil.error("打开文件夹失败");
                    }
                } else {
                    NotifyUtil.error("文件夹不存在");
                }

            }
        });

        startButton.addActionListener(e -> {
            textArea1.setText("");
            String dir = textField2.getText();
            if(StringUtils.isBlank(dir)){
                NotifyUtil.error("服务目录不能为空");
                return;
            }
            port = Convert.toInt(textField1.getText(), 5257);
            if (StringUtils.isNotBlank(dir)) {
                File file = new File(dir);
                if (file.exists()) {
                    try {

                        SimpleServer simpleServer = HttpUtil.createServer(port).setRoot(file);
                        simpleServer.start();
                        httpServer = simpleServer.getRawServer();
                        textArea1.setText("Running at http://" + NetUtil.getLocalhost().getHostName() + ":" + port);
                    } catch (Exception ex) {
                        NotifyUtil.error("服务启动失败");
                    }
                } else {
                    NotifyUtil.error("文件夹不存在");
                }

            }
        });

        stopButton.addActionListener(e -> {
            textArea1.setText("");
            if(httpServer != null){
                httpServer.stop(0);
                textArea1.setText("服务成功停止。。");
            }
        });

        downloadButton.addActionListener(e -> {
            textArea1.setText("");
            String dir = dirText.getText();
            String url = urlText.getText();
            if (StringUtils.isBlank(dir)) {
                NotifyUtil.error("请先选择源码存放位置");
                return;
            }
            if (StringUtils.isBlank(url)) {
                NotifyUtil.error("请输入目标网址");
                return;
            }
            try {
                String dirName = url.replace("http://", "");
                dirName = dirName.replace("https://", "");
                dirName = dirName.substring(0,dirName.lastIndexOf(":"));
                if (dirName.endsWith("/")) {
                    dirName = dirName.substring(0, dirName.lastIndexOf("/"));
                }
                if (!dir.endsWith("/")) {
                    dir = dir + "/" + dirName;
                }
                WebSourceDownloadUtil.downloadHtmlSources(url, dir);
                textArea1.setText("下载成功,目录为:"+dir);
                NotifyUtil.info("下载成功");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });
    }

    public JPanel getContent() {
        return jPanel;
    }
}
